﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class TimKiemSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }

            if (!IsPostBack)
            {
                getData();
            }
            else
            {
                if (Request.Params["btn_Tim"] != null)
                {
                    getData();
                }                
            }
        }

        private void getData()
        {

            IServerWeb sw = new ServerWeb();
            SanPham_DAO dao = new SanPham_DAO(sw.Connection);
            LoaiSanPham_DAO daoLSP = new LoaiSanPham_DAO(sw.Connection);
            string TenSanPham = Request.Params["TenSanPham"];
            if (TenSanPham == null)
                TenSanPham = "";
            List<SanPham> listData = dao.getListByKey_OrderMSP(TenSanPham);
            for (int i = 0; i < listData.Count; i++)
            {
                listData[i].CongDung = (listData[i].CoHang ? "Có" : "Không");
                listData[i].ThanhPhan = daoLSP.getByKey(listData[i].MaLoaiSanPham).TenLoaiSanPham;                
            }

            Session.Add("page_TimKiemSP_dataSP", listData);
            data_GridView.EditIndex = -1;
            Bind_Data();


            sw.closeConnection();

        }

        private void Bind_Data()
        {
            data_GridView.DataSource = Session["page_TimKiemSP_dataSP"];
            data_GridView.DataBind();
        }

        protected void data_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            data_GridView.PageIndex = e.NewPageIndex;
            Bind_Data();
        }
    }
}